package controllers

import (
	"boomlive.com/boomlive-common/constants"
	"boomlive.com/boomlive-common/utils"
	"github.com/beego/beego/v2/core/logs"
	beego "github.com/beego/beego/v2/server/web"
)

type BaseController struct {
	beego.Controller
	LanguageCode string
	RequestId    string
}

func (c *BaseController) Prepare() {
	//解析语言
	c.ParseLanguage()

	//设置requestId
	c.setRequestId()
}

func (c *BaseController) ParseLanguage() {
	c.LanguageCode = utils.GetLanguageCode(c.Ctx)
}

func (c *BaseController) setRequestId() {
	//通过请求接口地址和uuid生成requestId
	reqId := c.Ctx.Input.GetData("requestId")
	if reqId != nil {
		c.RequestId = reqId.(string)
	}
}

func (c *BaseController) Error(f interface{}, v ...interface{}) {
	v = append(v, "requestId="+c.RequestId)
	logs.Error(f, v...)
}

func (c *BaseController) Warn(f interface{}, v ...interface{}) {
	v = append(v, "requestId="+c.RequestId)
	logs.Warn(f, v...)
}

func (c *BaseController) Info(f interface{}, v ...interface{}) {
	v = append(v, "requestId="+c.RequestId)
	logs.Info(f, v...)
}

func (c *BaseController) Debug(f interface{}, v ...interface{}) {
	v = append(v, "requestId="+c.RequestId)
	logs.Debug(f, v...)
}

func (c *BaseController) Result(code constants.ErrNo, data interface{}) {
	c.Data["json"] = utils.GenerateResultBody(code, data, c.LanguageCode)
	_ = c.ServeJSON()
}
